//
//  SubViewController.swift
//  SwiftLearning
//
//  Created by 黄龙 on 2023/12/19.
//

import UIKit

class SubViewController: BasicNavibarVC {
    let tableView:UITableView = {
        let table = UITableView()
        return table
    }()
    
    var systemColors: [UIColor] = [
        .systemRed,
        .systemGreen,
        .systemBlue,
        .systemOrange,
        .systemYellow,
        .systemPink,
        .systemPurple,
        .systemTeal,
        .systemIndigo,
        .systemBrown,
        .systemGray,
        .systemGray2,
        .systemGray3,
        .systemGray4,
        .systemGray5,
        .systemGray6,
        .label,
        .secondaryLabel,
        .tertiaryLabel,
        .quaternaryLabel,
        .link,
        .placeholderText,
        .separator,
        .opaqueSeparator,
        .systemBackground,
        .secondarySystemBackground,
        .tertiarySystemBackground,
        .systemGroupedBackground,
        .secondarySystemGroupedBackground,
        .tertiarySystemGroupedBackground,
        .systemFill,
        .secondarySystemFill,
        .tertiarySystemFill,
        .quaternarySystemFill,
        .lightText,
        .darkText
    ]
    var colorTitles: [String] = [
        ".systemRed",
        ".systemGreen",
        ".systemBlue",
        ".systemOrange",
        ".systemYellow",
        ".systemPink",
        ".systemPurple",
        ".systemTeal",
        ".systemIndigo",
        ".systemBrown",
        ".systemGray",
        ".systemGray2",
        ".systemGray3",
        ".systemGray4",
        ".systemGray5",
        ".systemGray6",
        ".label",
        ".secondaryLabel",
        ".tertiaryLabel",
        ".quaternaryLabel",
        ".link",
        ".placeholderText",
        ".separator",
        ".opaqueSeparator",
        ".systemBackground",
        ".secondarySystemBackground",
        ".tertiarySystemBackground",
        ".systemGroupedBackground",
        ".secondarySystemGroupedBackground",
        ".tertiarySystemGroupedBackground",
        ".systemFill",
        ".secondarySystemFill",
        ".tertiarySystemFill",
        ".quaternarySystemFill",
        ".lightText",
        ".darkText"
    ]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.navigationItem.setRightBarButton(nil, animated: false)
    }
    
    override func initView() {
        if #available(iOS 15.0, *) {//系统版15以上才有的颜色
            systemColors.insert(contentsOf: [.systemMint,.systemCyan,.tintColor], at: 10) //插在[9]systemBrown之后
            colorTitles.insert(contentsOf: [".systemMint",".systemCyan",".tintColor"], at: 10)
        } else if #available(iOS 13.0, *){
            
        }else{
            systemColors.append(.groupTableViewBackground)
            colorTitles.append("groupTableViewBackground")
        }
        for i in 0 ..< systemColors.count{
            NSLog("[%d].%@", i,systemColors[i].description)
            //[0].<UIDynamicCatalogSystemColor: 0x282da6600; name = systemRedColor>
            //or [12].<UITintColor: 0x28389ff00>
            //or [37].UIExtendedGrayColorSpace 1 0.6
        }
        
        self.workView.addSubview(tableView)
        tableView.frame = workView.bounds
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 44
//        tableView.sectionHeaderHeight = 24 //无效
        tableView.separatorStyle = .none //默认即为singleLine(有分隔线)
//        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "normalTableCell")
    }

}


extension SubViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return systemColors.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //normal-cell:
        var cell = tableView.dequeueReusableCell(withIdentifier: "normalTableCell")//从缓存池中取
        //dequeueReusableCell(withIdentifier:)调用前不需要注册"normalTableCell"；
        //如果注册了，就会返回默认cell，从而走不到下面UITableViewCell.init，设置.subtitle风格的cell
        //而如果是用dequeueReusableCell(withIdentifier:,for:)须注册，否则异常
        if nil == cell{//如缓存池中没取到，则创建
            cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "normalTableCell")
//style设置cell的样式
//.default(没有detailText),.subtitle(有subtitle,在title下面),.value1(右侧有detailText)
        }
//        let iSection = indexPath.section
        let iRow = indexPath.row
        cell?.contentView.backgroundColor = systemColors[iRow]
        if systemColors[iRow] == .label{
            cell?.textLabel?.textColor = .lightGray
        }else{
            cell?.textLabel?.textColor = .label
        }
        cell?.textLabel?.text = colorTitles[iRow]
//        cell?.detailTextLabel?.text = "colro.name" //显示副标题
        cell?.imageView?.image = UIImage(named: "login-userico")?.scaleImage(toSize: CGSize(width: 20, height: 24)) //显示左侧图标
//        cell?.accessoryType = .disclosureIndicator //右侧显示小箭头
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    
}
